var balanceClueUI = function() {
    var self = this;
    self.list = [];
};
balanceClueUI.prototype = {};

balanceClueUI.prototype.init = function() {
    var self = this;
    self.restore();

    $(".oper-clue .close").click(function() {
        $(".oper-clue").hide();
    });
    if (!Rights.check("/account/log/pass", context.user.user.grade || '0')) return;

    // 定期拉取数据
    Util.registerTimer("BALANCE_CLUE_UI", 10, function() {
        self.loop();
    });
}

balanceClueUI.prototype.loop = function() {
    var self = this;
    CMD.query_account_log({
        pageSize: 10,
        state: 0,
        type: "deposit,withdraw,penalty,reward"
    }, function(r) {
        var n = [];
        for (var i in r.list) {
            var d = r.list[i];
            if (d.type == 'deposit' && d.channel != 'handOper') continue;
            if (self.list.indexOf(d.id) != -1) continue;
            if (new Date().getTime() - new Date(d.start).getTime() > 12 * 3600 * 1000) continue;
            self.list.push(d.id);
            n.push(d);
        }
        if (n.length == 0) return;

        // 填充内容
        self.save();
        var c = "";
        var template = '<div>__time__ __title__<br/>用户：__user__，金额：<span class="red">__amount__</span></div>';
        for (var i in r.list) {
            var d = r.list[i];
            var l = template;
            if (d.type == 'deposit') {
                l = l.replace(/__title__/, "充值/" + Util.convertChannel(d.channel2));
            }
            else if (d.type == 'withdraw') {
                l = l.replace(/__title__/, "取款请求");
            }
            else if (d.type == 'penalty') {
                l = l.replace(/__title__/, "扣款请求");
            }
            else if (d.type == 'reward') {
                l = l.replace(/__title__/, "奖励请求");
            }
            l = l.replace(/__user__/, d.user.mobile || '游客');
            l = l.replace(/__amount__/, (d.amount/100).toFixed(2));
            l = l.replace(/__time__/, d.start.substring(5));
            c += l;
        }
        $(".oper-clue .c").html(c);
        $(".oper-clue").show();

        // 声音提醒
        var audio = $("#music")[0];
        audio.currentTime  = 0;
        audio.play();
    });
}

balanceClueUI.prototype.restore = function() {
    var self = this;
    try {
        if (localStorage) {
            var v = localStorage.getItem("com.cp-zy-balance");
            if (v) self.list = v.split(",");
        }
    }
    catch(e) {
        console.error(e);
    }
}

balanceClueUI.prototype.save = function() {
    var self = this;
    if (self.list.length > 50) {
        self.list.splice(0, self.list.length - 50);
    }
    try {
        if (localStorage) localStorage.setItem("com.cp-zy-balance", self.list.join(","));
    }
    catch(e) {
        console.error(e);
    }
}

window.BalanceClueUI = new balanceClueUI();